<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <link rel="stylesheet" href="${base}/resources/css/main.css" />
    <script type='text/javascript' src='${base}/resources/js/lib/jquery.1.11.3.min.js'></script>
    <script type='text/javascript' src='${base}/resources/js/lib/jquery.md5.js'></script>
    <script type='text/javascript' src='${base}/resources/js/common.js'></script>
</head>
<body>
<!-- 内容 -->
<div class='content'>
    <div class="login">
        <div class="banner"></div>
        <form id="registerForm" method="post">
            <div  class="loginInp">
                <p>亿金融业务管理系统</p>
                <input name="password" type="hidden"/>
	            <div class="cf">
                    <input type='text' class="input" id="username" name="username" placeholder='请输入用户名'
                           onblur="_changeUniqueValue('username')">
                    <span id="error_username" class="error"></span>
                </div>
                <div class="cf">
                    <input type='text' class="input" id="name" name="name" placeholder='请输入公司名称'
                           onblur="_changeUniqueValue('name')">
                    <span id="error_name" class="error"></span>
                </div>
                <div class="cf">
                    <input type='password' class="input" id="passwords" name="passwords" placeholder='密码长度为6-20位'
                           onblur="_changeValue('passwords')">
                    <span id="error_passwords" class="error"> </span>
                </div>
                <div class="cf">
                    <input type='password' class="input" id="companyPassword" name="companyPassword" placeholder='请再次输入密码'
                           onblur="_changeValue('companyPassword')">
                    <span id="error_companyPassword" class="error"> </span>
                </div>
                <div class="cf">
                    <input type='text' class="input" id="mail" name="mail" placeholder='请输入邮箱'
                           onblur="_changeUniqueValue('mail')">
                    <span id="error_mail" class="error"> </span>
                </div>
                <div class="cf">
                    <input class="input" type='text' id="address" name="address" placeholder='请输入公司地址'
                           onblur="_changeValue('address')">
                    <span id="error_address" class="error"> </span>
                </div>
                <div class="cf">
                    <input type='text' class="input" id="contactNumber" name="contactNumber" placeholder='请输入联系电话'
                           onblur="_changeUniqueValue('contactNumber')"
                    >
                    <span id="error_contactNumber" class="error"> </span>
                </div>
                <div class="btn">
                    <button type='button' id="register" class="registerbtn">注册</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script type="text/javascript">
    var base = "${base}";
    $(function () {
        $("#register").click(function () {
            if (_changeUniqueValue("username")==false) {
                return false;
            }
            if (_changeUniqueValue("name")==false) {
                return false;
            }
            if (_changeUniqueValue("mail") ==false) {
                return false;
            }
            if (_changeUniqueValue("contactNumber") ==false) {
                return false;
            }
            if (_changeValue("passwords") ==false) {
                return false;
            }
            if (_changeValue("companyPassword")==false) {
                return false;
            }
            if (_changeValue("address")==false) {
                return false;
            }
            $.ajax({
                url: base + '/passport/doRegister',
                type: 'post',
                data: $("#registerForm").serialize(),
                success: function (data) {
                    if (data.success) {
                        window.location.href = base + "/index"
                    } else {
                        alert(data.msg);
                    }
                }
            });
        })
    });
    function _changeUniqueValue(of) {
        var inputValue = $("#" + of).val();
        var inputData = {};
        $("#error_" + of).text("");
        if (of == "username") {
            if ($.isBlank(inputValue)) {
                $("#error_" + of).text("用户名不能为空");
                return false;
            }
            inputData = {username: inputValue};
        } else if (of == "name") {
            if ($.isBlank(inputValue)) {
                $("#error_" + of).text("公司名不能为空");
                return false;
            }
            $("#errorName").text("");
            inputData = {name: inputValue};
        } else if (of == "mail") {
            if ($.isBlank(inputValue)) {
                $("#error_" + of).text("邮箱不能为空");
                return false;
            }
            if (!$.isEmail(inputValue)) {
                $("#error_" + of).text("邮箱格式错误");
                return false;
            }
            inputData = {mail: inputValue};
        } else if (of == "contactNumber") {
            if ($.isBlank(inputValue)) {
                $("#error_" + of).text("联系电话不能为空");
                return false;
            }
            if (!$.isNumber(inputValue) || inputValue.length < 6) {
                $("#error_" + of).text("联系电话格式错误");
                return false;
            }
            inputData = {contactNumber: inputValue};
        }
        //异步判断，用户名是否存在
        $.ajax({
            url: base + '/passport/check',
            type: 'post',
            data: inputData,
            success: function (data) {
                if (data.success) {
                    $("#error_" + of).text("");
                } else {
                    $("#error_" + of).text(data.msg);
                    return false;
                }
            }
        })

    }
    function _changeValue(of) {
        var inputValue = $("#" + of).val();
        $("#error_" + of).text("");
        if (of == "passwords") {
            if ($.isBlank(inputValue)) {
                $("#error_" + of).text("密码不能为空");
                return false;
            }
            if (inputValue.length < 6 || inputValue.length > 20) {
                $("#error_" + of).text("密码长度在6-20位");
                return false;
            }
            $("input[name='password'").val($.md5(inputValue));
        } else if (of == "companyPassword") {


            if ($.isBlank(inputValue)) {
                $("#error_" + of).text("确认密码不能为空");
                return false;
            }
            var password = $("#passwords").val();
            if (password != inputValue) {
                $("#error_" + of).text("两次密码不一致");
                return false;
            }
        } else if (of == "address") {
            if ($.isBlank(inputValue)) {
                $("#error_" + of).text("地址不能为空");
                return false;
            }
        }
    }
</script>
</body>
</html>